package com.party.admin.task;

import com.party.core.model.member.PartnerMember;
import com.party.core.service.member.IPartnerMemberService;
import com.party.core.service.member.biz.MemberPersonalCountBizService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 个人数据统计表数据初始化/矫正
 * @Author: yangshoukun
 * @Date: 2018/2/28 15:45
 */
@Component("quartzMemberPersonalCount")
public class QuartzMemberPersonalCount {

    private static Logger logger = LoggerFactory.getLogger(QuartzMemberPersonalCount.class);

    @Autowired
    private MemberPersonalCountBizService memberPersonalCountBizService;
    @Autowired
    private IPartnerMemberService partnerMemberService;

    public void init() {
        try {
            logger.info("个人数据统计表数据初始化开始");
            /*// 统计活动报名的数量和金额
            memberActCount(STATUS_INIT);
            // 统计众筹报名的数量和金额
            memberZcCount(STATUS_INIT);
            // 统计活动和众筹的联合发起数量
            memberRepresentCount(STATUS_INIT);
            // 统计众筹支持金额
            memberSupportCount(STATUS_INIT);
            // 初始化partner_member有,member_personal_count中没有的
            partnerMember();*/
            PartnerMember partnerMember1 = new PartnerMember();
//            partnerMember1.setPartnerId("2704afd9b700440d93cba9f6534b34f3");
//            partnerMember1.setMemberId("ca819181137042f398a8693945b66162");
            List<PartnerMember> list = partnerMemberService.listPage(partnerMember1, null);
            if (CollectionUtils.isEmpty(list)) {
                return;
            }

            list.forEach(partnerMember -> {
                memberPersonalCountBizService.countOne(partnerMember);
            });
            logger.info("个人数据统计表数据初始化结束");
        } catch (Exception e) {
            logger.error("个人数据统计表数据初始化异常", e);
        }
    }

    public void check() {
        try {
            logger.info("个人数据统计表数据矫正开始");
            /*// 统计活动报名的数量和金额
            memberActCount(STATUS_CHECK);
            // 统计众筹报名的数量和金额
            memberZcCount(STATUS_CHECK);
            // 统计活动和众筹的联合发起数量
            memberRepresentCount(STATUS_CHECK);
            // 统计众筹支持金额
            memberSupportCount(STATUS_CHECK);*/
            logger.info("个人数据统计表数据矫正结束");
        } catch (Exception e) {
            logger.error("个人数据统计表数据矫正异常", e);
        }
    }

    /**
     * 初始化的时候将partner_member里面, 切不在personal_count里面的数据, 初始化到personal_count里面
     */
    /*private void partnerMember() {
        List<PartnerMember> list = partnerMemberService.listPage(new PartnerMember(), null);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        list.forEach(partnerMember -> {
            memberPersonalCountBizService.getSet(partnerMember.getPartnerId(), partnerMember.getMemberId());
        });
    }

    *//**
     * 统计活动报名的数量和金额
     *//*
    private void memberActCount(Integer status) {
        List<Map<String, Object>> list = memberActService.getAllSuccessGroup(status);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<CountEntity> entities = list.stream().map(map -> {
            CountEntity entity = new CountEntity();
            entity.setPartnerId(map.get("partnerId") == null ? null : map.get("partnerId").toString());
            entity.setMemberId(map.get("memberId") == null ? null : map.get("memberId").toString());
            entity.setCreateDate(map.get("createDate") == null ? null : DateUtils.convertDate(map.get("createDate").toString()));
            entity.setActivityNum(map.get("activityNum") == null ? null : Integer.parseInt(map.get("activityNum").toString()));
            entity.setActivityMoney(map.get("activityMoney") == null ? null : BigDecimal.valueOf(Double.valueOf(map.get("activityMoney").toString())));
            return entity;
        }).collect(Collectors.toList());

        entities.forEach(entity -> {
            MemberPersonalCount temp = new MemberPersonalCount();
            temp.setCreateDate(entity.getCreateDate());
            temp.setActivityNum(entity.getActivityNum());
            temp.setActivityMoney(entity.getActivityMoney());
            memberPersonalCountBizService.insertMemberCount(entity.getPartnerId(), entity.getMemberId(), temp);
        });

    }

    *//**
     * 统计众筹报名数量和金额
     *//*
    private void memberZcCount(Integer status) {
        List<Map<String, Object>> list = projectService.getAllGroup(status);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<CountEntity> entities = list.stream().map(map -> {
            CountEntity entity = new CountEntity();
            entity.setPartnerId(map.get("partnerId") == null ? null : map.get("partnerId").toString());
            entity.setMemberId(map.get("memberId") == null ? null : map.get("memberId").toString());
            entity.setCreateDate(map.get("createDate") == null ? null : DateUtils.convertDate(map.get("createDate").toString()));
            entity.setCrowdfundNum(map.get("crowdfundNum") == null ? null : Integer.parseInt(map.get("crowdfundNum").toString()));
            entity.setCrowdfundMoney(map.get("crowdfundMoney") == null ? null : BigDecimal.valueOf(Double.valueOf(map.get("crowdfundMoney").toString())));
            return entity;
        }).collect(Collectors.toList());

        entities.forEach(entity -> {
            MemberPersonalCount temp = new MemberPersonalCount();
            temp.setCreateDate(entity.getCreateDate());
            temp.setCrowdfundNum(entity.getCrowdfundNum());
            temp.setCrowdfundMoney(entity.getCrowdfundMoney());
            memberPersonalCountBizService.insertMemberCount(entity.getPartnerId(), entity.getMemberId(), temp);
        });
    }

    *//**
     * 统计联合发起数量(活动和众筹一起)
     *//*
    private void memberRepresentCount(Integer status) {
        List<Map<String, Object>> list = distributorRelationService.getAllGroup(status);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<CountEntity> entities = list.stream().map(map -> {
            CountEntity entity = new CountEntity();
            entity.setPartnerId(map.get("partnerId") == null ? null : map.get("partnerId").toString());
            entity.setMemberId(map.get("memberId") == null ? null : map.get("memberId").toString());
            entity.setCreateDate(map.get("createDate") == null ? null : DateUtils.convertDate(map.get("createDate").toString()));
            entity.setRepresentNum(map.get("representNum") == null ? null : Integer.parseInt(map.get("representNum").toString()));
            return entity;
        }).collect(Collectors.toList());

        entities.forEach(entity -> {
            MemberPersonalCount temp = new MemberPersonalCount();
            temp.setCreateDate(entity.getCreateDate());
            temp.setRepresentNum(entity.getRepresentNum());
            memberPersonalCountBizService.insertMemberCount(entity.getPartnerId(), entity.getMemberId(), temp);
        });
    }

    *//**
     * 支持金额的统计
     *//*
    private void memberSupportCount(Integer status) {
        List<Map<String, Object>> list = supportService.getAllGroup(status);
        if (CollectionUtils.isEmpty(list)) {
            return;
        }

        List<CountEntity> entities = list.stream().map(map -> {
            CountEntity entity = new CountEntity();
            entity.setPartnerId(map.get("partnerId") == null ? null : map.get("partnerId").toString());
            entity.setMemberId(map.get("memberId") == null ? null : map.get("memberId").toString());
            entity.setCreateDate(map.get("createDate") == null ? null : DateUtils.convertDate(map.get("createDate").toString()));
            entity.setSupportMoney(map.get("supportMoney") == null ? null : BigDecimal.valueOf(Double.valueOf(map.get("supportMoney").toString())));
            return entity;
        }).collect(Collectors.toList());

        entities.forEach(entity -> {
            MemberPersonalCount temp = new MemberPersonalCount();
            temp.setCreateDate(entity.getCreateDate());
            temp.setSupportMoney(entity.getSupportMoney());
            memberPersonalCountBizService.insertMemberCount(entity.getPartnerId(), entity.getMemberId(), temp);
        });
    }*/
}

